import { Module } from '@nestjs/common';
import { UserModule } from '../system/user/user.module';
import { UploadController } from './upload.controller';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { join, extname } from 'path';

@Module({
  imports: [
    UserModule,
    MulterModule.register({
      storage: diskStorage({
        destination: join(__dirname, 'dist', '../images'),
        filename: (_, file, cb) => {
          const fileName = `${new Date().getTime() + extname(file.originalname)}`;
          return cb(null, fileName);
        }
      }),
      fileFilter: (_, file, cb) => {
        if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
          cb(null, true);
        } else {
          cb(new Error('Only .jpeg and .png files are allowed'), false);
        }
      }
    })
  ],
  controllers: [UploadController],
  providers: []
})
export class UploadModule {}
